﻿using System;
using System.Collections.Generic;
using ProjScan.Tools;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Submodules.Effects_Play_Check
{
	// Token: 0x02000196 RID: 406
	internal class RendererSetting
	{
		// Token: 0x06000DD6 RID: 3542 RVA: 0x0003E518 File Offset: 0x0003C718
		public static void Initialize()
		{
			RendererSetting.cameraSettings = default(RendererSetting.CameraProperties);
			RendererSetting.mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
			RendererSetting.overdrawCamera = GameObject.Find("OverdrawCamera").GetComponent<Camera>();
			RendererSetting.cameras.Add(RendererSetting.mainCamera);
			RendererSetting.cameras.Add(RendererSetting.overdrawCamera);
			ApiCompatibilityUtils.Instance.RegisterEditorExitPlayMode(new Action(RendererSetting.ResetWorkArea));
		}

		// Token: 0x06000DD7 RID: 3543 RVA: 0x00006FFA File Offset: 0x000051FA
		private static void ResetWorkArea()
		{
			RendererSetting.ClearEditorUpdates();
		}

		// Token: 0x06000DD8 RID: 3544 RVA: 0x0003E590 File Offset: 0x0003C790
		public static void SetParticleSystemDrawer(ParticleSystemDrawer particleSystemDrawer)
		{
			bool isPlaying = Application.isPlaying;
			if (isPlaying)
			{
				RendererSetting.particleModel = particleSystemDrawer.modelGameObject;
				RendererSetting.particleDrawer = particleSystemDrawer;
				bool flag = RendererSetting.particleDrawer != null;
				if (flag)
				{
					RendererSetting.bounds = RendererSetting.particleDrawer.currentBounds;
				}
				bool flag2 = RendererSetting.particleModel == null || RendererSetting.particleDrawer == null;
				if (flag2)
				{
					throw new Exception("Try to watch a particle system without Box");
				}
				RendererSetting.particleModel.transform.position = Vector3.zero;
				RendererSetting.particleModel.transform.eulerAngles = Vector3.zero;
				RendererSetting.mainCamera.transform.position = Vector3.zero;
				RendererSetting.mainCamera.transform.eulerAngles = Vector3.zero;
				EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.update, new EditorApplication.CallbackFunction(RendererSetting.UpdateCameraRendering));
			}
		}

		// Token: 0x06000DD9 RID: 3545 RVA: 0x00007003 File Offset: 0x00005203
		public static void ClearEditorUpdates()
		{
			EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(RendererSetting.UpdateCameraRendering));
		}

		// Token: 0x06000DDA RID: 3546 RVA: 0x0003E678 File Offset: 0x0003C878
		private static void UpdateCameraRendering()
		{
			RendererSetting.bounds = RendererSetting.particleDrawer.currentBounds;
			RendererSetting.aspectRatio = RendererSetting.bounds.size.x / RendererSetting.bounds.size.y;
			Vector3 size = RendererSetting.bounds.size;
			float num = float.Parse(size.x.ToString("f3"));
			size = RendererSetting.bounds.size;
			float num2 = float.Parse(size.y.ToString("f3"));
			size = RendererSetting.bounds.size;
			float num3 = float.Parse(size.z.ToString("f3"));
			bool flag = num > 0f && num2 > 0f && num3 > 0f;
			if (flag)
			{
				RendererSetting.cameraSettings = new RendererSetting.CameraProperties
				{
					ViewingRect = ((RendererSetting.aspectRatio > 1f) ? new Rect(0f, 0f, 1f, 1f / RendererSetting.aspectRatio) : new Rect(0f, 0f, RendererSetting.aspectRatio, 1f)),
					NearClipPlane = 0f,
					FarClipPlane = RendererSetting.bounds.size.z * 5f,
					Position = new Vector3(RendererSetting.bounds.center.x, RendererSetting.bounds.center.y, -RendererSetting.bounds.size.z * 2f),
					OrthographicSize = RendererSetting.bounds.size.y / 2f
				};
				RendererSetting.UpdateCameraParameters();
			}
		}

		// Token: 0x06000DDB RID: 3547 RVA: 0x0003E82C File Offset: 0x0003CA2C
		private static void UpdateCameraParameters()
		{
			for (int i = 0; i < RendererSetting.cameras.Count; i++)
			{
				RendererSetting.cameras[i].rect = RendererSetting.cameraSettings.ViewingRect;
				RendererSetting.cameras[i].nearClipPlane = RendererSetting.cameraSettings.NearClipPlane;
				RendererSetting.cameras[i].farClipPlane = RendererSetting.cameraSettings.FarClipPlane;
				RendererSetting.cameras[i].transform.position = RendererSetting.cameraSettings.Position;
				RendererSetting.cameras[i].orthographicSize = RendererSetting.cameraSettings.OrthographicSize;
			}
		}

		// Token: 0x040008A0 RID: 2208
		private static Camera mainCamera;

		// Token: 0x040008A1 RID: 2209
		private static Camera overdrawCamera;

		// Token: 0x040008A2 RID: 2210
		private static GameObject particleModel;

		// Token: 0x040008A3 RID: 2211
		private static ParticleSystemDrawer particleDrawer;

		// Token: 0x040008A4 RID: 2212
		private static Bounds bounds;

		// Token: 0x040008A5 RID: 2213
		private static float aspectRatio;

		// Token: 0x040008A6 RID: 2214
		private static RendererSetting.CameraProperties cameraSettings;

		// Token: 0x040008A7 RID: 2215
		private static List<Camera> cameras = new List<Camera>();

		// Token: 0x02000197 RID: 407
		private struct CameraProperties
		{
			// Token: 0x040008A8 RID: 2216
			public Rect ViewingRect;

			// Token: 0x040008A9 RID: 2217
			public float NearClipPlane;

			// Token: 0x040008AA RID: 2218
			public float FarClipPlane;

			// Token: 0x040008AB RID: 2219
			public Vector3 Position;

			// Token: 0x040008AC RID: 2220
			public float OrthographicSize;
		}
	}
}
